<?php /* Copyright 2014 Karl R. Wilcox

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

class references {
  private $dom;
  private $xpath;
  
  function __construct($xml) {
    if ( is_string($xml) ) {
      $this->dom = new DOMDocument;
      $this->dom->Loadxml($xml);  
    } elseif ( is_a($xml,'DOMDocument')) 
      $this->dom = $xml;
    else
      echo "internal -> cannot figure out xml data type for format";
      $this->xpath = new DOMXPath($this->dom);
  }

  public function setIDREFs(&$errorList = null, $errorFormat = 'text') { 
    $refList = array (
      array ( '//charge[@type="same/same"]','ancestor::charge[1]|ancestor::ordinary[1]|preceding::charge[1]|preceding::ordinary[1]' ), // two of the same | another ...
      array ( '//tincture[@origin="ofsame"]','ancestor::tincture[1]|preceding::tincture[1]' ),// Of the same
      array ( '//tincture[@origin="pending"]','following::tincture[1]' ),// Pending
      array ( '//tincture[@origin="offield"]','ancestor::simple/tincture[1]' ),// Of the field
      array ( '//tincture[@origin="offirst"]','ancestor::simple//tincture[1]' ),// of the first
      array ( '//tincture[@origin="ofsecond"]','ancestor::simple//tincture[2]' ),// of the second
      array ( '//tincture[@origin="ofthird"]','ancestor::simple//tincture[3]' ),// of the third
      array ( '//tincture[@origin="offourth"]','ancestor::simple//tincture[4]' ),// of the fourth
      array ( '//charge[@number="asmany"]','ancestor::charge[1]|preceding::charge[1]' ), // TODO should really be ordinary also
      );                                                                                 // but only if number > 1
    foreach ( $refList as $ref ) {
      $nodeList = $this->xpath->query($ref[0]);
      for ( $i = 0; $i < $nodeList->length; $i++ ) {
        $sourceNode = $nodeList->item($i);
        $refNodes = $this->xpath->query($ref[1],$nodeList->item($i));

        if ( $refNodes->length ) {
          $targetID = $refNodes->item(0)->getAttribute('ID');
          $sourceNode->setAttribute('IDREF',$targetID);
        }
 elseif ( $errorList) {
          $sourceID = $sourceNode->getAttribute('ID');
          switch ( $errorFormat ) {
          case 'text':
            $errorList .= "Cannot find what Node ($sourceID) refers to\n";
            break;
          case 'html':
            $errorList .= "<p>Cannot find what Node <strong>($sourceID)</strong> refers to</p>\n";
            break;
          }
        }
      }
    }     
    return $this->dom;
  }
}
   
?>
